﻿#include <iostream>
#include <vector>
using namespace std;

class Solution
{
public:
    // 121. 买卖股票的最佳时机
    int maxProfitOne(vector<int>& prices)
    {
        int prevMin = 0x3f3f3f3f, ret = 0;
        for (int i = 0; i < prices.size(); ++i)
        {
            prevMin = min(prevMin, prices[i]);
            ret = max(ret, prices[i] - prevMin);
        }
        return ret;
    }

    // 122. 买卖股票的最佳时机 II
    // 双指针
    /*int maxProfit(vector<int>& prices)
    {
        int left = 0, right = 0, ret = 0, n = prices.size();
        while (right < n)
        {
            while (right + 1 < n && prices[right + 1] > prices[right])
                right++;
            ret += prices[right] - prices[left];
            right++;
            left = right;
        }
        return ret;
    }*/

    // 拆分交易
    int maxProfit(vector<int>& prices)
    {
        int ret = 0;
        for (int i = 1; i < prices.size(); ++i)
        {
            if (prices[i] > prices[i - 1])
                ret += prices[i] - prices[i - 1];
        }
        return ret;
    }
};

int main()
{
    /*vector<int> v{ 7,1,5,3,6,4 };
    cout << Solution().maxProfitOne(v) << endl;*/

    vector<int> v{ 7,1,5,3,6,4 };
    cout << Solution().maxProfit(v) << endl;

    return 0;
}